/*****************************************************************************/ /*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _SYSTASK_MODULE_
#include "bsp_api.h"
#include "pincfg.h"
#include "os_obj.h"
#include "sys_task.h"
#include "common.h"
#include "sys_init.h"
#include "swap.h"

/*****************************************************************************
* @brief   fun.
*****************************************************************************/
void sys_led_ctr(void);
int core_main(void);

/*****************************************************************************
* @brief   var.
*****************************************************************************/


/*****************************************************************************
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(const void *argument)
{
	sys_app_init();
	
	thread_create();
	
	bsp_spi_init();
	
	while (1)
	{
		Gst.u_tick = app_tick_get();
		pulse_creat(&Gst.pulse, Gst.u_tick);
		
		sys_app_run_once();
		
		// 按键采集
		Gst.key.pulse_sample = 1;
		Gst.key.pulse_time = Gst.pulse.pulse_100ms;
		io_flt_ext(&Gst.key, 3, key.in_st());
		
		// 按键动作
		if(Gst.key.edge_found)
		{
			dbg_print("key press down ! \n");
		}
		if(Gst.key.edge_lost)
		{
			dbg_print("key release  ! \n");
		}
		
		#if COREMARK_EN > 0
		if(Gst.b_coremark)
		{
			Gst.b_coremark = 0;
			core_main();
		}
		#endif
		
        osDelay(5);
	}
}

#if COREMARK_EN > 0
/*******************************************************************************
 * @brief: 功能示例
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int coremark(int argc, char *argv[])
{
	dbg_puts("coremark is start! wait 30s!\n");
	Gst.b_coremark = 1;
	
	return 0;
}
DBG_FUN_EXPORT(coremark);
#endif

/*****************************************************************************
*
* @brief   led control.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_led_ctr(void)
{
	static word_tt st = {1};

	if(Gst.pulse.pulse_20ms)
	{
		led1.out(0);
		led2.out(0);
		led3.out(0);
		led4.out(0);
	}
	
	if (Gst.pulse.pulse_200ms)
	{
		st.uval = bit16_roll_shift_left(st.uval, 5, 1);

		led1.out(st.bits.b0);
		led2.out(st.bits.b1);
		led3.out(st.bits.b2);
		if	(st.bits.b3)
			led4.en();
		else
			led4.dis();
		
		//spiss.tog();
	}
}
obj_app_order(sys_led_ctr);

/*****************************************************************************
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(void)
{

	dbg_puts(CR SYMBOL_LINE_EQ CR);
	
	dbg_puts(asc_pic_light);
	
	dbg_puts(SYMBOL_LINE_EQ CR);
	dbg_print("HCLK = %d MHZ, PCLK1 = %d MHZ, PCLK2 = %d MHZ\n",
			  rcu_clock_freq_get(CK_AHB) / 1000000,
			  rcu_clock_freq_get(CK_APB1)/ 1000000,
			  rcu_clock_freq_get(CK_APB2)/ 1000000);
	dbg_puts(SYMBOL_LINE_EQ CR);
	
}
obj_init_order5(system_info);


/*******************************************************************************
 * @brief: 功能示例
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int spi_wr(int argc, char *argv[])
{
	int wlen, rlen = 0;
	uint8_t buff[32];
	
	if(argc < 2)
	{
		dbg_puts("arg is few" PTCR);
	}

	if(argc >= 3)
		rlen = atoi(argv[2]);
	
	rang_limit(rlen, 0, 31);
	
	wlen = ascii_to_hex2_multi(argv[1], buff, 32);
	rang_limit(wlen, 1, 31);
	
	bsp_spi_cs(0);
	
	if(wlen)
	{
		dbg_print("spi send data amount %d" PTCR, wlen);
		bsp_spi_write(buff, wlen);
	}

	if(rlen)
	{
		bsp_spi_read(buff, rlen);
		mem_u8_disp("RD DAT:" PTCR, buff, rlen, 16);
	}
	
	bsp_spi_cs(1);
	
	dbg_puts(SYMBOL_LINE_EQ PTCR);
	
	return 0;
}
DBG_FUNX_EXPORT(spi_wr, "whex rlen");

int spi_rd(int argc, char *argv[])
{
	int  rlen = 0;
	uint8_t buff[32];
	
	if(argc < 2)
	{
		dbg_puts("arg is few" PTCR);
	}

	rlen = atoi(argv[1]);
	
	rang_limit(rlen, 1, 31);
	
	bsp_spi_cs(0);

	if(rlen)
	{
		bsp_spi_read(buff, rlen);
		mem_u8_disp("RD DAT:" PTCR, buff, rlen, 16);
	}
	
	bsp_spi_cs(1);
	
	dbg_puts(SYMBOL_LINE_EQ PTCR);
	
	return 0;
}
DBG_FUNX_EXPORT(spi_rd, "rlen");


